package com.binhthuan.catechist.client.data;

import com.binhthuan.catechist.shared.constant.Parameter;
import com.binhthuan.catechist.shared.constant.Service;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.FieldValueExtractor;
import com.smartgwt.client.data.XMLTools;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;

public class ParentDS extends AbstractDS {

    private static ParentDS instance;

    private ParentDS() {
        super(Service.PARENT);

        DataSourceIntegerField idField = new DataSourceIntegerField(Parameter.ID);
        idField.setHidden(true);
        idField.setPrimaryKey(true);

        DataSourceTextField careerField = new DataSourceTextField(Parameter.CAREER);

        DataSourceTextField firstNameField = new DataSourceTextField(Parameter.FIRST_NAME);

        DataSourceTextField lastNameField = new DataSourceTextField(Parameter.LAST_NAME);

        DataSourceBooleanField genderField = new DataSourceBooleanField(Parameter.MALE);

        DataSourceTextField patronSaintField = new DataSourceTextField(Parameter.PATRON_SAINT);

        DataSourceTextField phoneField = new DataSourceTextField(Parameter.PHONE);

        DataSourceTextField fullName = new DataSourceTextField(Parameter.FULL_NAME);
        fullName.setFieldValueExtractor(new FieldValueExtractor() {
            
            @Override
            public Object execute(Object record, Object value, DataSourceField field, String fieldName) {
                String patronSaint = XMLTools.selectObjects(record, Parameter.PATRON_SAINT).getJavaScriptObject().toString();
                String lastName = XMLTools.selectObjects(record, Parameter.LAST_NAME).getJavaScriptObject().toString();
                String firstName = XMLTools.selectObjects(record, Parameter.FIRST_NAME).getJavaScriptObject().toString();
                return patronSaint + " " + lastName + " " + firstName;
            }
        });

        setFields(idField, careerField, firstNameField, lastNameField, genderField, patronSaintField, phoneField, fullName);
    }

    public static ParentDS getInstance() {
        if (instance == null) {
            instance = new ParentDS();
        }
        return instance;
    }
}
